//
//  WebViewController.m
//  半糖
//
//  Created by RenShuang on 15-9-10.
//  Copyright (c) 2015年 shuang. All rights reserved.
//

#import "WebViewController.h"
#import "BaseHeader.h"

@interface WebViewController ()<UIWebViewDelegate>

@property(nonatomic,retain)UIWebView *myWebView;
@property(nonatomic,assign)NSInteger count;
@end

@implementation WebViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    if ([NightHandle shareHandle].temp == YES) {
        [_myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];
        [_myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
        
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
//    self.count = 0;
    _myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
//    _myWebView.delegate = self;
    _myWebView.scrollView.showsVerticalScrollIndicator = NO;
     NSString *url = [NSString stringWithFormat:@"http://m.ibantang.com/product/detail/%@/",self.urlStr];
    
    
    
//    _myWebView.request = [[NSURLRequest alloc]initWithURL:url];
//    [_myWebView loadRequest:[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:url]]];
    [self.view addSubview:_myWebView];
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        NSString *str = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        [_myWebView loadHTMLString:str baseURL:[NSURL URLWithString:url]];
        
        for (UIView *aview in _myWebView.subviews) {
        
           
            
            NSLog(@"%@",aview.class);
        }

        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败 === %@",error);
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前网络不好" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
            //[alertView release];
        });
        
        
    }];
   
    
//    [_myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
    
    [self barCreate];
    
    
    
    

}


//- (void)webViewDidStartLoad:(UIWebView *)webView{
//
////    if (self.count != 0) {
////        [_myWebView stopLoading];
////    }
////    
////    self.count++;
//}


- (void)barCreate
{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    titleView.backgroundColor = [UIColor redColor];
    [self.view addSubview:titleView];
    [titleView release];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/3, 20, SCREEN_WIDTH/3, 44)];
    //titleLabel.backgroundColor = [UIColor greenColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = @"物品详情";
    //label加粗并放大
    titleLabel.font = [UIFont boldSystemFontOfSize:20.];
    [titleView addSubview:titleLabel];
    [titleLabel release];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(20, 30, 30, 25);
    [backButton setBackgroundImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [titleView addSubview:backButton];
    
//    UIButton *scButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    scButton.frame = CGRectMake(SCREEN_WIDTH-90, 25, 30, 30);
//    [scButton setBackgroundImage:[UIImage imageNamed:@"sc.png"] forState:UIControlStateNormal];
//    [scButton addTarget:self action:@selector(shoucang) forControlEvents:UIControlEventTouchUpInside];
//    [titleView addSubview:scButton];
    
//    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    shareButton.frame = CGRectMake(SCREEN_WIDTH-40, 25, 30, 30);
//    [shareButton setBackgroundImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateNormal];
//    [shareButton addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
//    [titleView addSubview:shareButton];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
